package com.tevintao.marketingsystem.task;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;


/**
 * 通过搜索添加好友，接受好友
 */
public class AddRecommendFriendTaskProcess extends SetTaskExtra {
    private static AddRecommendFriendTaskProcess ourInstance = new AddRecommendFriendTaskProcess();

    public static AddRecommendFriendTaskProcess getInstance() {
        return ourInstance;
    }

    private AddRecommendFriendTaskProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean contacts = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_CONTACTS);
            if (contacts) {
                WxUiViewConfig.printViewGroup((ViewGroup) v);
                View newItem = v.findViewById(WxUiViewConfig.ID_ACCEPT_FRIEND_LIST_ITEM);
                if (null != newItem) {
                    ViewOperation.clickView(newItem);
                } else {
                    View newFriend = v.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM);
                    if (null != newFriend) {
                        ViewOperation.clickView(newFriend);
                    }
                }
            }

        } else if (WxUiViewConfig.VIEW_NEW_FRIEND_LIST.equals(contextName)) {
            View friendList = v.findViewById(2131625708);

            LogManager.getInstance().logInfo("friendList = " + friendList);
            if (friendList instanceof ListView) {
                ViewGroup vg = (ViewGroup) friendList;
                for (int i = 0; i < vg.getChildCount(); i++) {
                    View addFriend = vg.getChildAt(i).findViewById(WxUiViewConfig.ADD_FRIEND);
                    if (null != addFriend && addFriend.getVisibility() == View.VISIBLE) {
                        ViewOperation.clickView(addFriend);

                        break; //一次只点击一个 添加
                    } else {
                        WxUiViewConfig.printViewGroup((ViewGroup) vg.getChildAt(i));
                    }
                }
            }

            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "添加完毕");
        }
        return false;
    }
}
